import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        name: '码农呆呆飞',
        students: [
            {
                name: '小红',
                age: 15
            },
            {
                name: '小黄',
                age: 17
            },
            {
                name: '小黑',
                age: 18
            },
            {
                name: '大黑',
                age: 20
            },
        ],
        num: 0,
        info: {
            name: '龚飞飞',
            age: 20,
            height: 180
        }
    },
    mutations: {
        changeName(state) {
            state.name = '啊飞飞'
        },
        add(state, value) {
            state.num += value
        },
        addWeight(state) {
            Vue.set(state.info, 'weight', 120)
        }
    },
    getters: {
        moreAge(state) {
            return function (age) {
                return state.students.filter(item => item.age > age)
            }
        }
    },
    actions: {
        addNumAsync() {
            setTimeout(() => {
                context.commit("addNum", obj)
            }, 2000)
        }
    }
})

export default store

